//
//  BaseViewController.m
//
//
//  Created by Fidetro on 16/9/22.
//  Copyright © 2016年 Fidetro. All rights reserved.
//

#import "BaseViewController.h"

#define SystemVersionIOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 ? YES : NO)


#ifdef SystemVersionIOS7
#define BASE_TEXTSIZE(text, font) ([text length] > 0 ? [text sizeWithAttributes:@{NSFontAttributeName:font}] : CGSizeZero)
#else
#define BASE_TEXTSIZE(text, font) ([text length] > 0 ? [text sizeWithFont:font] : CGSizeZero)
#endif

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //顶部不留空
//    self.automaticallyAdjustsScrollViewInsets = NO;
//    //取消半透明
//    self.navigationController.navigationBar.translucent = NO;
    //设置背景颜色
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BG"]];
    
    self.navigationBar = [DTCNavigationBarView getNavigationView:self.view];
//    [self.navigationController setNavigationBarHidden:NO];//这句如果设成Yes会导致侧滑失效
    [self.navigationController.navigationBar setHidden:YES];
    //设置导航栏图片
    self.navigationBar.backgroundColor = [UIColor blackColor];
    [self setLeftButtonImage:[UIImage imageNamed:@"back_icon"]];
    [self masLayoutSubview];
    
    self.view.backgroundColor = [UIColor whiteColor];

    [self refreshBaseControlValue];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}


- (void)hideNavigationBottomLine
{
    [self.navigationBar setLineViewHidden:YES];
}

- (void)hideNavigationBar
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    
    self.navigationController.navigationBar.translucent = YES;
}

/**
 状态栏变白 要设置info.plist View controller-based status bar appearance  为NO
 */
-(UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleDefault;
    
}

#pragma mark - 设置NavigationTitle
- (void)setNavigationTitle:(NSString *)title titleColor:(UIColor *)titleColor
{
    [self.navigationBar setTitle:title textColor:titleColor];
}

- (void)setNavigationBarTitle:(NSString *)title
{
    [self setNavigationTitle:title titleColor:[UIColor whiteColor]];
}
#pragma mark - 左侧按钮设置
- (void)setLeftButtonTitle:(NSString *)title{
    [self.leftButton setTitle:title forState:UIControlStateNormal];
    [self.leftButton setImage:[UIImage imageNamed:@"title_icon_back"] forState:UIControlStateNormal];
    [self.leftButton sizeToFit];
    [self.leftButton layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:0];
    [self.navigationBar setLeftBarButtonItem:self.leftButton];
}

- (void)setLeftButtonImage:(UIImage *)image{
    [self.leftButton setTitle:@"" forState:UIControlStateNormal];
    [self.leftButton setImage:image forState:UIControlStateNormal];
    [self.leftButton setFrame:CGRectMake(0, 0, image.size.width,image.size.height)];
    [self.leftButton sizeToFit];
    [self.navigationBar setLeftBarButtonItem:self.leftButton];
}
#pragma mark - 右侧按钮设置
- (void)setRightButtonTitle:(NSString *)title
{
    [self.rightButton setTitle:title forState:UIControlStateNormal];
    [self.rightButton sizeToFit];
    [self.navigationBar setRightBarButtonItem:self.rightButton];
}

- (void)setRightButtonImage:(UIImage *)image
{
    [self.rightButton setTitle:@"" forState:UIControlStateNormal];
    [self.rightButton setImage:image forState:UIControlStateNormal];
    [self.rightButton sizeToFit];
    [self.navigationBar setRightBarButtonItem:self.rightButton];
}
#pragma mark - --------------------------touch Event--------------------------
- (void)onLeftButtonClick:(id)sender
{
    if ([self.navigationController.visibleViewController isMemberOfClass:[UITabBarController class]]) {
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onRightButtonClick:(id)sender
{
    
}

-(void)touchesEnded:(NSSet<UITouch*> *)touches withEvent:(UIEvent*)event
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];//点击屏幕收起键盘
}
#pragma mark - --------------------------get方法--------------------------
- (UIButton *)leftButton
{
    if (!_leftButton) {
        _leftButton = [[UIButton alloc]init];
        [_leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [_leftButton setExclusiveTouch:YES];
        [_leftButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:17.f];
        [_leftButton addTarget:self action:@selector(onLeftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _leftButton;
}

- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [[UIButton alloc]init];
        [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [_rightButton setExclusiveTouch:YES];
        [_rightButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:17.f];
        
        [_rightButton addTarget:self action:@selector(onRightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _rightButton;
}

- (void)masLayoutSubview
{
    
}

- (void)setNavigationStyle
{
    
}

- (void)refreshBaseControlValue
{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    NSLog(@"\n\n dealloc : %@  \n\n     \n",[self class]);
}
@end
